$(document).ready(function() {
	// click handler
	var bbclick = function(event){
		calc();
	};
	
	var openraiseclick = function(event){
		calc();	
	};

	var threebetclick = function(event){
		calc();
	};
	
	// select menus
	var bbvalues = ["25","20","16","13","12","11","10"];
	var bbSelectMenu = createSelectMenu("bb","25",bbvalues,bbclick);
	$("#bb").append(createFilterLabel("BB"));
	$("#bb").append(bbSelectMenu);	
	
	
	var openraisevalues = ["100","90","80","70","65","60","55","50","45","40"];
	var openraiseSelectMenu = createSelectMenu("openraise","70",openraisevalues,openraiseclick);
	$("#openraise").append(createFilterLabel("Openraise"));
	$("#openraise").append(openraiseSelectMenu);	
	
	var threebetvalues = ["50","45","40","35","30","25","20","15","10"];
	var threebetSelectMenu = createSelectMenu("threebet","25",threebetvalues,threebetclick);
	$("#threebet").append(createFilterLabel("3-Bet"));
	$("#threebet").append(threebetSelectMenu);
	
	function move(arr,val,dir){
		var arrLength = arr.length;
		var pos = null;
		for(i = 0; i < arrLength; i++){
			if(arr[i] == val)
				pos = i;
		}

		if(dir == "up") {
				pos -= 1;
				if(pos < 0)
					pos = arrLength - 1;
		} else {
			pos += 1;
			if(pos >= arrLength)
				pos = 0;
		}
		return arr[pos];
	}
	
	// plus and minus buttons
	var bbplus = $(document.createElement('div')).attr({
		"class": "plusimg",
	});
	
	var bbminus = $(document.createElement('div')).attr({
		"class": "minusimg",
	});
	
	$(bbplus).click(function(){
		$("#select_bb").html(move(bbvalues,$("#select_bb").html(),"up"));
		calc();
	});
	
	$(bbminus).click(function(){
		$("#select_bb").html(move(bbvalues,$("#select_bb").html(),"down"));
		calc();
	});
	
	$("#bb").append(bbplus);
	$("#bb").append(bbminus);
	
	var openraiseplus = $(document.createElement('div')).attr({
		"class": "plusimg",
	});
	
	var openraiseminus = $(document.createElement('div')).attr({
		"class": "minusimg",
	});
		
	$(openraiseplus).click(function(){
		$("#select_openraise").html(move(openraisevalues,$("#select_openraise").html(),"up"));
		calc();
	});
	
	$(openraiseminus).click(function(){
		$("#select_openraise").html(move(openraisevalues,$("#select_openraise").html(),"down"));
		calc();
	});
	
	$("#openraise").append(openraiseplus);
	$("#openraise").append(openraiseminus);
	
	var threebetplus = $(document.createElement('div')).attr({
		"class": "plusimg",
	});
	
	var threebetminus = $(document.createElement('div')).attr({
		"class": "minusimg",
	});
	
	$(threebetplus).click(function(){
		$("#select_threebet").html(move(threebetvalues,$("#select_threebet").html(),"up"));
		calc();
	});
	
	$(threebetminus).click(function(){
		$("#select_threebet").html(move(threebetvalues,$("#select_threebet").html(),"down"));
		calc();
	});
	
	$("#threebet").append(threebetplus);
	$("#threebet").append(threebetminus);
	
	// filer
	$(".filter").click(function(){
		if($(this).hasClass("active") == true)
			$(this).removeClass("active");
		else
			$(this).addClass("active");
		calc();
	});
	
	$("html").keyup(function(event){
		 if(event.which == 78){
			    if($(".nash").css("display") != "none"){
			    	$(".nash").hide();
			    	$("#playimg").show();
			    }
			    	
			    else {
			    	$("#playimg").hide();
			    	$(".nash").show();
			    }			
		 }  
	});
	
	calc();
});

function calc(){
	$(".rangelabel").remove();
	$(".range").remove();
	
	if($("#oopip").hasClass("active") == true) {
		var source = null;
		switch($("#select_bb").html()){
			case "25":
				source = "img/reg1.png"
				break;
			case "20":
				source = "img/reg2.png"
				break;
			case "16":
				source = "img/reg3.png"
				break;
			case "13":
				source = "img/reg4.png"
				break;
			case "12":
				source = "img/reg4.png"
				break;
			case "11":
				source = "img/reg5.png"
				break;
			case "10":
				source = "img/reg5.png"
				break;
		}
		$("#playimg").attr("src",source);
	} else {
		var source = null;
		switch($("#select_bb").html()){
			case "25":
				source = "img/fish1.png"
				break;
			case "20":
				source = "img/fish2.png"
				break;
			case "16":
				source = "img/fish3.png"
				break;
			case "13":
				source = "img/fish4.png"
				break;
			case "12":
				source = "img/fish4.png"
				break;
			case "11":
				source = "img/fish5.png"
				break;
			case "10":
				source = "img/fish5.png"
				break;
		}
		$("#playimg").attr("src",source);
	}
	
	$.getJSON('data/ranges.json', function(data) {
		if($("#pt").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("Perfect Pushing Range:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data[$("#select_bb").html()]["openraise"][$("#select_openraise").html()]["pt"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#pp").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("Playable Pushing Range:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data[$("#select_bb").html()]["openraise"][$("#select_openraise").html()]["pp"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#pe").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("Edge Pushing Range:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data[$("#select_bb").html()]["openraise"][$("#select_openraise").html()]["pe"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#n3p").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("NAI3B Playable:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data["nonaitt"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#n3e").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("NAI3B Edge:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data["nonaiedge"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#threet").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("Raise/Call Top:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data[$("#select_bb").html()]["threebet"][$("#select_threebet").html()]["threet"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#threett").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("Raise/Call TT+:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data[$("#select_bb").html()]["threebet"][$("#select_threebet").html()]["threett"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
		
		if($("#threeat").hasClass("active") == true) {
			var label = $(document.createElement('div')).attr({
				"class": "rangelabel"
			}).html("Raise/Call TT+/AT+:");
			
			var range = $(document.createElement('div')).attr({
				"class": "range"
			}).html(data[$("#select_bb").html()]["threebet"][$("#select_threebet").html()]["threeat"]);
			
			$("#wrap").append(label);
			$("#wrap").append(range);
		}
	});
}
